package cn.puge.mertisfront.controller;


import cn.puge.commonutils.R;
import cn.puge.mertisfront.entity.PugeContents;
import cn.puge.mertisfront.entity.PugeMer;
import cn.puge.mertisfront.entity.merVo.meRetVo;
import cn.puge.mertisfront.entity.queryVo.MerQueryVo;
import cn.puge.mertisfront.service.PugeContentsService;
import cn.puge.mertisfront.service.PugeMerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 申报
 * @author LIJW
 * @since 2020-08-28
 */
@RestController
@RequestMapping("/mertisfront/puge-mer")
@CrossOrigin
public class PugeMerController {

    @Autowired
    private PugeMerService pugeMerService;

    @Autowired
    private PugeContentsService pugeContentsService;


    /**
     * 条件查询当前成员所有申报
     * @param userId
     * @param merQueryVo
     * @return
     */
    @PostMapping("getAllMertis")
    public R getAllMertis(@RequestParam("id") String userId,
                          @RequestBody(required = false) MerQueryVo merQueryVo){

        List<PugeMer> list1 = pugeMerService.getAllMertisQuery(merQueryVo,userId);

        List<meRetVo> list = new ArrayList<>();
        for (PugeMer pugeMer : list1){
            meRetVo meRetVo = new meRetVo();
            String pugeTitleId = pugeMer.getPugeTitle();
            PugeContents pugeContents = pugeContentsService.getById(pugeTitleId);
            BeanUtils.copyProperties(pugeMer,meRetVo);
            meRetVo.setPugeTitle(pugeContents.getConame());
            meRetVo.setGread(pugeContents.getConGread());
            list.add(meRetVo);
        }
        return R.ok().data("list",list);
    }



    /**
     * 查询出所有申报
     * @param id
     * @return
     */
    @GetMapping("allMertis")
    public R allMertis(@RequestParam("id") String id){
        QueryWrapper<PugeMer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",id);
        queryWrapper.orderByDesc("gmt_create");
        List<PugeMer> alllist1 = pugeMerService.list(queryWrapper);

        List<meRetVo> alllist = new ArrayList<>();

        for (PugeMer pugeMer : alllist1){
            meRetVo meRetVo = new meRetVo();
            String pugeTitleId = pugeMer.getPugeTitle();
            PugeContents pugeContents = pugeContentsService.getById(pugeTitleId);
            BeanUtils.copyProperties(pugeMer,meRetVo);
            meRetVo.setPugeTitle(pugeContents.getConame());
            meRetVo.setGread(pugeContents.getConGread());
            alllist.add(meRetVo);
        }

        return R.ok().data("alllist",alllist);
    }



    /**
     * 申报考评
     * @param pugeMer
     * @return
     */
    @PostMapping("addMerti")
    public R addMerti(@RequestBody PugeMer pugeMer){

        pugeMerService.addMer(pugeMer);

        return R.ok();
    }



}

